package com.yizhi.student.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.yizhi.student.dao.StudentInfoDao;
import com.yizhi.student.domain.StudentInfoDO;
import com.yizhi.student.service.StudentInfoService;



@Service
public class StudentInfoServiceImpl implements StudentInfoService {



	@Autowired
	private StudentInfoDao studentInfoDao;
	
	@Override
	public StudentInfoDO get(Integer id){
		System.out.println("======service层中传递过来的id参数是：" + id + "======");
		return studentInfoDao.get(id);
	}


	@Override
	public List<StudentInfoDO> list(Map<String, Object> map){
		Object currPage =  map.get("currPage");
		Integer currpage = Integer.parseInt((String) currPage);
		Object pageSize =  map.get("pageSize");
		Integer parseInt = Integer.parseInt((String) pageSize);
		int offset = (currpage - 1) * parseInt;
		Object name = map.get("name");
		String newName = "%"+name+"%";
		System.out.println(newName);
		Object tocollegeId = map.get("tocollegeId");
		Object tomajorId =  map.get("tomajorId");
		Object classId =  map.get("classId");
		return studentInfoDao.list(offset, parseInt,newName,tocollegeId, tomajorId, classId);
	}

	//"===================================================================================="


	@Override
	public int count(Map<String, Object> map){
		Object currPage =  map.get("currPage");
		Integer currpage = Integer.parseInt((String) currPage);
		Object pageSize =  map.get("pageSize");
		Integer parseInt = Integer.parseInt((String) pageSize);
		return studentInfoDao.count(currpage, parseInt);
	}
	
	@Override
	public int save(StudentInfoDO studentInfo){
		if(studentInfo == null) {
			throw new IllegalArgumentException("不能为空");
		}
		int save = studentInfoDao.save(studentInfo);
		if(save != 1) {
			throw new RuntimeException("存储失败");
		}
		return save;
	}
	
	@Override
	public int update(StudentInfoDO studentInfo){
		if(studentInfo == null) {
			throw new IllegalArgumentException("不能为空");
		}
		int update = studentInfoDao.update(studentInfo);
		if(update != 1) {
			throw new RuntimeException("存储失败");
		}
		return update;
	}
	
	@Override
	public int remove(Integer id){
		if(id == null) {
			throw new IllegalArgumentException("删除错误");
		}
		int remove = studentInfoDao.remove(id);
		if(remove != 1) {
			throw new RuntimeException("失败");
		}
		return remove;
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		if(ids == null) {
			throw new IllegalArgumentException("删除错误");
		}
		int batchRemove = studentInfoDao.batchRemove(ids);
		return batchRemove;
	}
	
}
